#include "StdAfx.h"
#include "XMLReader.h"



using namespace System;
using namespace System::Xml;
using namespace System::Data::SQLite;

XMLReader::XMLReader(void)
{

}
void XMLReader::Work(String^ table_name, String^ database)
{
	SqlMaster^G_Master = gcnew SqlMaster;
	array<String^>^ xml_attribute = {""};
	array<String^>^attribute_name = {""};
	XmlTextReader^ reader = gcnew XmlTextReader("sample_xml.xml");
	SQLiteConnection ^ connection = gcnew SQLiteConnection(String::Format("Data Source={0}; Version=3;", database));
	connection->Open();
	//command->ExecuteNonQuery();
	if (reader->ReadToDescendant(table_name)){
		while ( reader->Read() && reader->Name != table_name ) {
			if (reader->Name == "item") {
				Array::Resize(xml_attribute,reader->AttributeCount);
				Array::Resize(attribute_name,reader->AttributeCount);
				for (int i = 0; i < xml_attribute->Length; i++){
					xml_attribute[i] = reader->GetAttribute(i);
			    }
				for (int i = 0; i <attribute_name->Length; i++){
					reader->MoveToAttribute(i);
					attribute_name[i] = reader->Name;
			    }
				G_Master->DoSQL(connection,G_Master->GetSqlInsert(table_name,attribute_name,xml_attribute));

		    }
	    }
     }
	connection->Close();
}